package at.punkt.sentimentanalysis.webservices;

/**
 *
 * @author lejtovicz
 */
import at.punkt.sentimentanalysis.api.SentimentAnalysisRequest;
import at.punkt.sentimentanalysis.api.SentimentAnalysisResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.simba.utils.tree.SentenceSplitter;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.simba.utils.tree.Sentiment;
import org.simba.utils.tree.StanfordParser;

@Controller
public class SentimentController {

    //  private final Logger logger = Logger.getLogger(SentimentController.class);
    private Sentiment sentiment;
    private String defaultSentimentTemplate = "formoutput";

    public void setDefaultSentimentTemplate(String defaultSentimentTemplate) {
        this.defaultSentimentTemplate = defaultSentimentTemplate;
    }

    public Sentiment getSentiment() {
        return sentiment;
    }

    public void setSentiment(Sentiment sentiment) {
        this.sentiment = sentiment;
    }

    @RequestMapping(value = "/api/sentiment", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView sentiment(SentimentAnalysisRequest request) throws Exception {

        ModelAndView mav = new ModelAndView(defaultSentimentTemplate);
        String text = request.getText();
        String entity = request.getEntity().trim();
        
        if (text == null || entity == null) {
            throw new Exception("text, entity are mandatory parameters!");
        }


        SentenceSplitter sSplitter = new SentenceSplitter();
        List<String> sentences = sSplitter.doSentenceSplitting(text);
       
        HashMap<String, Double> scoreList = new HashMap<String, Double>();
        SentimentAnalysisResponse resp = new SentimentAnalysisResponse();
        for (String sentence : sentences) {
            String[] words = sentence.split("\\s+");
            List<String> wordList = Arrays.asList(words);
            if (wordList.contains(entity)) {
                String parseTree = StanfordParser.getDependencies2(sentence, "c:\\Users\\lejtovicz\\Downloads\\stanford-parser-2011-09-14\\grammar\\englishFactored.ser.gz");
                String role = getRole(parseTree, entity);
                Double score = sentiment.getSentiment(parseTree.trim(), entity, role);
                scoreList.put(sentence, score);
            } else {
                scoreList.put(sentence, null);
            }
        }
        resp.setRequest(request);
        resp.setResults(scoreList);

        mav.addObject(resp);
        return mav;
    }

    private String getRole(String parseTree, String entity) {
//   nsubj(happy-4, SAP-1)
//cop(happy-4, is-2)
//advmod(happy-4, really-3)
//root(ROOT-0, happy-4)
//aux(receive-6, to-5)
//xcomp(happy-4, receive-6)
//det(grant-8, this-7)
//dobj(receive-6, grant-8)
        String role = null;
        //  String[] parseLines = parseTree.split("\\)");

        String regex = "(.*)\\(.*\\-\\d+\\,\\s+" + entity + "\\-\\d+\\)";
        Pattern p = Pattern.compile(regex);
//        for (int i = 0; i < parseLines.length; i++) {
//            String parseLine = parseLines[i] + ")";
//            Matcher m = p.matcher(parseLine); // get a matcher object
//            if (m.matches()) {
//                roles.add(m.group(1));
//            }
//
//        }
        Matcher m = p.matcher(parseTree); // get a matcher object
        if (m.matches()) {
            role = m.group(1);
        }

        return role;
    }
}
